package Gather.ExerciseDome;

import java.util.*;

/**
 * ClassName:Exercise006
 * Package:Gather.ExerciseDome
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/2/21-20:35
 * @Version:v1.0
 */
public class Exercise006 {
    /*集合嵌套练习*/
    /*需求
    * 定义一个Map集合,键用表示省份名称province,值表示市city,但是市会有多个
    *
    * 添加完毕后,遍历结果格式如下：
    * 江苏省=南京市,扬州市,苏州市,无锡市,常州市
    * 湖北省=武汉市,孝感市,十堰市,宜昌市,鄂州市
    * 河北省=石家庄市,唐山市,邢台市,保定市,张家口市*/
    public static void main(String[] args) {
        //创建Map集合
        HashMap<String, ArrayList<String>> hm = new HashMap<>();
        //创建单列集合存储市的信息
        ArrayList<String> city1 = new ArrayList<>();
        Collections.addAll(city1,"南京市","扬州市","苏州市","无锡市","常州市");

        ArrayList<String> city2 = new ArrayList<>();
        Collections.addAll(city2,"武汉市","孝感市","十堰市","宜昌市","鄂州市");

        ArrayList<String> city3 = new ArrayList<>();
        Collections.addAll(city3,"石家庄市","唐山市","邢台市","保定市","张家口市");

        //把省份和多个市添加到map集合中
        hm.put("江苏省",city1);
        hm.put("湖北省",city2);
        hm.put("河北省",city3);

        Set<Map.Entry<String, ArrayList<String>>> entries = hm.entrySet();
        //外循环:遍历entries得到键值对对象
        for (Map.Entry<String, ArrayList<String>> entry : entries) {
            //entry依次表示每一个键值对对象
            String key = entry.getKey();
            ArrayList<String> value = entry.getValue();
            StringJoiner sj = new StringJoiner(", ","","");
            //内循环:遍历每一个ArrayList得到每一个城市
            for (String city : value) {
                sj.add(city);
            }
            System.out.println(key+" = "+sj);
        }
    }
}
